//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.qst.crop.controller;

import com.qst.crop.common.Result;
import com.qst.crop.entity.Reserve;
import com.qst.crop.service.ReserveService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.*;

import java.util.Iterator;
import java.util.List;

@Api(
        tags = {"预约模块接口"}
)
@RestController
@RequestMapping({"/reserve"})
@CrossOrigin
public class ReserveController {
    @Autowired
    private ReserveService reserveService;

    public ReserveController() {
    }

    @ApiOperation("根据ID查询预约情况")
    @GetMapping({"/selectId/{id}"})
    public Result selectById(@PathVariable("id") Integer id) {
        Reserve reserve = this.reserveService.selectById(id);
        return new Result(true, 20000, "查询成功", reserve);
    }

    @ApiOperation("添加预约情报")
    @PostMapping({"/add"})
    public Result add(@RequestBody Reserve reserve, BindingResult bindingResult) {
        UserDetails principal = (UserDetails)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String name = principal.getUsername();
        reserve.setQuestioner(name);
        if (!bindingResult.hasErrors()) {
            this.reserveService.insert(reserve);
            return new Result(true, 20000, "添加成功");
        } else {
            StringBuffer stringBuffer = new StringBuffer();
            List<ObjectError> allErrors = bindingResult.getAllErrors();
            Iterator var7 = allErrors.iterator();

            while(var7.hasNext()) {
                ObjectError objectError = (ObjectError)var7.next();
                stringBuffer.append(objectError.getDefaultMessage()).append("; ");
            }

            String s = stringBuffer.toString();
            System.out.println(s);
            return new Result(false, 20001, "添加失败", s);
        }
    }

    @ApiOperation("根据id修改预约情报")
    @PutMapping({"/update"})
    public Result update(@RequestBody Reserve reserve, BindingResult bindingResult) {
        if (!bindingResult.hasErrors()) {
            this.reserveService.updateById(reserve);
            return new Result(true, 20000, "修改成功");
        } else {
            StringBuffer stringBuffer = new StringBuffer();
            List<ObjectError> allErrors = bindingResult.getAllErrors();
            Iterator var5 = allErrors.iterator();

            while(var5.hasNext()) {
                ObjectError objectError = (ObjectError)var5.next();
                stringBuffer.append(objectError.getDefaultMessage()).append("; ");
            }

            String s = stringBuffer.toString();
            System.out.println(s);
            return new Result(false, 20001, "修改失败", s);
        }
    }

    @ApiOperation("根据id删除预约情报")
    @DeleteMapping({"/delete/{id}"})
    public Result delete(@PathVariable("id") Integer id) {
        this.reserveService.delete(id);
        return new Result(true, 20000, "删除成功");
    }

    @ApiOperation("根据用户查询预约情况")
    @GetMapping({"/selectByKind/{kind}"})
    public Result selectByKind(@PathVariable("kind") String kind) {
        List<Reserve> reserves = this.reserveService.selectByReserve(kind);
        return new Result(true, 20000, "查询成功", reserves);
    }
}
